package DALFacades;

import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Date;

import Backwards.DBBackward;
import Backwards.DBBackward.TableCRUD;
import DomainLayer.*;
import Helpers.ApplicationLogger;
import Helpers.MD5Helper;

public class ConcreteFacadeServer2 implements IFacade {

	@Override
	public UserEntity GetUserEntityByLoginAndPassword(String login,
			String password) throws Throwable {
		ApplicationLogger.getInstance().LOG.info("Login: " + login
				+ " Password: " + password);

		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(new UserEntity());
		Collection<AbstractEntity> result = cCRUD
				.Select("select * from USER_TABLE where ID=15");
		UserEntity user = null;
		if (result.isEmpty() == false) {
			user = (UserEntity) result.iterator().next();
			ApplicationLogger.getInstance().LOG
					.info("User ID: " + user.getId());
		}

		// UserEntity user = new UserEntity();
		// user.setId(1);
		// user.setUserInfo(new UserInfoValueObject(null, "Ivan", "Mahabaat",
		// "Johnsovich", "vasya", "vasya", Gender.male, 15));
		// user.setUserState(new UserStateValueObject(null, false,
		// "q1w32d13as1f3asd", "127.0.0.1"));

		return user;
	}

	

   
	@Override
	public UserEntity GetUserById(int id) {
		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(new UserEntity());
		Collection<AbstractEntity> result = cCRUD
				.Select("select * from USER_TABLE where id=" + id);
		UserEntity user = null;
		if (!result.isEmpty()) {
			user = (UserEntity) result.iterator().next();
			return user;
		}
		return null;
	}

	@Override
	public UserEntity AuthentificationByLoginAndPassword(String login,
			String password) throws Throwable {
		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(new UserEntity());
		Collection<AbstractEntity> result = cCRUD
				.Select("select ut.* from User_TABLE ut join UserInfo_TABLE uit on ut.userinfo = uit.id where uit.Login='"
						+ login + "'");
		UserEntity user = null;
		if (!result.isEmpty()) {
			user = (UserEntity) result.iterator().next();
			String md5password = MD5Helper.getMD5(password
					+ user.getUserInfo().getSalt());
			ApplicationLogger.getInstance().LOG.info("MD5" + md5password);
			if (md5password.equalsIgnoreCase(user.getUserInfo().getPassword()))
				return user;
		}
		return null;
	}

	@Override
	public Boolean SetFeedBackEntity(String message, String from, String page)
			throws Throwable {
		ApplicationLogger.getInstance().LOG.info("new Feedback on: " + page
				+ " from: " + from);
		FeedBackEntity feedback = new FeedBackEntity(message, from, page);
		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(feedback);
		cCRUD.Create();
		return true;
	}

	@Override
	public Collection<AbstractEntity> GetFeedBackEntity() throws Throwable {
		ApplicationLogger.getInstance().LOG
				.info("Select all FeedBack from data base");
		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(new FeedBackEntity());
		Collection<AbstractEntity> result = cCRUD
				.Select("select * from FeedBack_TABLE");
		if (!result.isEmpty()) {
			return result;
		} else{
			ApplicationLogger.getInstance().LOG.info("No one feedback found!");
			return null;
		}
	}

	@Override
	public Boolean UpdateUser(UserEntity user) throws Throwable {
		ApplicationLogger.getInstance().LOG.info("Updating user...");
		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(user);
		cCRUD.Update();
		return true;
	}

	@Override
	public Boolean AddUser(String firstName, String secondName,
			String middleName, String login, String password, String gender,
			Integer age, String sessionId, String lastIP) throws Throwable {
		ApplicationLogger.getInstance().LOG.info("new User with login : "
				+ login);
		Gender genderEnum = Gender.valueOf(gender.toLowerCase());
		;
		genderEnum = Gender.valueOf(gender.toLowerCase());
		UserStateValueObject userState = new UserStateValueObject();
		UserInfoValueObject userInfo = new UserInfoValueObject();
		UserEntity user = new UserEntity();

		userInfo = new UserInfoValueObject(userInfo, 1, firstName, secondName,
				middleName, login, password, null, genderEnum, age);
		userState = new UserStateValueObject(userState, 1, false, sessionId,
				lastIP);

		user.setUserInfo(userInfo);
		user.setUserState(userState);

		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(user);
		cCRUD.Create();

		return true;
	}

	@Override
	public Boolean ChangeUser(String login, String password, String firstName,
			String secondName, String middleName, String gender, Integer age)
			throws NoSuchAlgorithmException {
		ApplicationLogger.getInstance().LOG.info("Change User with login : "
				+ login);
		Gender genderEnum = null;
		if (gender != null) {
			genderEnum = Gender.valueOf(gender.toLowerCase());
		}
		DBBackward dbb = new DBBackward();
		DBBackward.TableCRUD cCRUD = dbb.new TableCRUD(new UserEntity());
		Collection<AbstractEntity> result = cCRUD
				.Select("select ut.* from User_TABLE ut join UserInfo_TABLE uit on ut.userinfo = uit.id where uit.Login='"
						+ login + "'");
		UserEntity user = null;
		if (!result.isEmpty()) {
			user = (UserEntity) result.iterator().next();
			UserInfoValueObject userInfo = user.getUserInfo();
			if (!password.equals("null")) {
				password = MD5Helper.getMD5(password + userInfo.getSalt());
			} else
				password = null;
			userInfo = new UserInfoValueObject(userInfo, userInfo.getId(),
					firstName, secondName, middleName, login, password, null,
					genderEnum, age);
			user.setUserInfo(userInfo);
			cCRUD = dbb.new TableCRUD(user);
			cCRUD.Update();
			return true;
		}
		return false;
	}
}
